﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls.Maps;

namespace Hangout.Client.Controls.Hangouts
{
    public partial class HangoutDetails : UserControl
    {


        Core.Location.CurrentLocation location;

        public int loadPoints;

        public int LoadPoints
        {
            get
            {
                try
                {
                    return loadPoints;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                    return 0;
                }
            }
            set
            {
                try
                {
                    loadPoints = value;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
                
            }
        }

        private void CheckLoadCompleted()
        {
            try
            {
                if (LoadPoints == 4)
                {
                    //raise loading completed event handler
                    if (Loaded != null)
                    {
                        Loaded(null, new EventArgs());
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        

        List<Pushpin> pins = new List<Pushpin>();

        public event EventHandler ViewLocationClicked;

        public System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> MyLocation { get; set; }

        public event EventHandler Loaded;

        void CurrentLocation_PositionChanged(System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e)
        {
            try
            {

                location.PositionChanged -= new Core.Location.CurrentLocation.PositionChangedHandler(CurrentLocation_PositionChanged);
                MyLocation = e;
                Pushpin p = new Pushpin();
                p.Location = e.Position.Location;
                LocationMAP.Children.Add(p);
                p.Content = "You're here";
                pins.Add(p);
                DisplayRect();
                LoadPoints++;
                CheckLoadCompleted();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
            
        }

        private void DisplayRect()
        {
            try
            {
                if (pins.Count == 2)
                {
                    LocationMAP.Dispatcher.BeginInvoke(() => LocationMAP.SetView(LocationRect.CreateLocationRect(pins.Select(o => o.Location).ToArray())));
                    LocationMapPanel.Visibility = System.Windows.Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        public HangoutDetails()
        {
            try
            {
                InitializeComponent();
                Load();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void Load()
        {
            try
            {
                LoadPoints = 0;
                //get current location.
                location = new Core.Location.CurrentLocation();
                location.PositionChanged += new Core.Location.CurrentLocation.PositionChangedHandler(CurrentLocation_PositionChanged);
                location.StartWatcher();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private MeetupService.MeetupData hangout;

        public MeetupService.MeetupData Hangout
        {

            get
            {
                try
                {
                    return hangout;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                    return new MeetupService.MeetupData();
                }
            }

            set
            {
                try
                {
                    if (value != null)
                    {
                        hangout = value;
                        LoadData();
                    }

                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
            }
        }

        private void LoadData()
        {
            try
            {
                Hangout.Hangout.HangoutDateTime = Hangout.Hangout.HangoutDateTime.ToLocalTime(); //convert to local time :)
                if (String.IsNullOrWhiteSpace(Hangout.Description))
                {
                    DescriptionSP.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    DescriptionSP.Visibility = System.Windows.Visibility.Visible;
                    DescriptionLBL.Text = Hangout.Description;
                }

                if (Hangout.Hangout.HangoutDateTime != null)
                {
                    DateCanvas.Visibility = System.Windows.Visibility.Visible;
                    if (Hangout.Hangout.HangoutDateTime.Date == DateTime.Now.Date)
                    {
                        DateLBL.Text = "Today";
                    }

                    else if (Hangout.Hangout.HangoutDateTime.Date.AddDays(-1) == DateTime.Now.Date)
                    {
                        DateLBL.Text = "Yesterday";
                    }

                    else if (Hangout.Hangout.HangoutDateTime.Date.AddDays(1) == DateTime.Now.Date)
                    {
                        DateLBL.Text = "Tomorrow";
                    }

                    else if (Hangout.Hangout.HangoutDateTime.Date > DateTime.Now.Date && Hangout.Hangout.HangoutDateTime.Date.AddDays(7) <= DateTime.Now.Date) //if its this week
                    {
                        DateLBL.Text = Hangout.Hangout.HangoutDateTime.DayOfWeek.ToString();
                    }
                    else
                    {
                        DateLBL.Text = Hangout.Hangout.HangoutDateTime.ToShortDateString();
                    }

                    TimeLBL.Text = Hangout.Hangout.HangoutDateTime.ToShortTimeString();
                }
                else
                {
                    DateCanvas.Visibility = System.Windows.Visibility.Collapsed;
                }

                if (String.IsNullOrWhiteSpace(hangout.Location.Address))
                {
                    LocationAddressPanel.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    LocationAddressPanel.Visibility = System.Windows.Visibility.Visible;
                    AddressLBL.Text = Hangout.Location.Address;
                }

                if (Hangout.Location.LocationID != null)
                {
                    Services.UserLocationServiceClient.GetLocationCompleted += new EventHandler<UserLocationService.GetLocationCompletedEventArgs>(client_GetLocationCompleted);
                    Services.UserLocationServiceClient.GetLocationAsync((int)hangout.Location.LocationID, Settings.Settings.FacebookAccessToken);
                }
                else
                {
                    LocationMapPanel.Visibility = System.Windows.Visibility.Collapsed;
                }
                LoadPoints++;
                CheckLoadCompleted();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        void client_GetLocationCompleted(object sender, UserLocationService.GetLocationCompletedEventArgs e)
        {
            try
            {
                LoadPoints++;


                if (e.Error == null)
                {
                    if (e.Result != null)
                    {
                        HangoutLocation = e.Result;
                        ViewMap(e);
                    }
                }
                else
                {
                    MessageBox.Show("Cant get the hangout location on the map", "Turbulence", MessageBoxButton.OK);
                }
                CheckLoadCompleted();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        private void ViewMap(UserLocationService.GetLocationCompletedEventArgs e)
        {
            try
            {
                // Create a pushpin to put at the center of the view.
                Pushpin pin1 = new Pushpin();
                pin1.Location = new System.Device.Location.GeoCoordinate(e.Result.Latitude, e.Result.Longitude);
                pin1.Content = "Hangout's here";
                LocationMAP.Children.Add(pin1);
                pins.Add(pin1);
                DisplayRect();
                LoadPoints++;
                CheckLoadCompleted();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
            
        }

        private void LocationMAP_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (ViewLocationClicked != null)
                {
                    ViewLocationClicked(null, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }



        public UserLocationService.Location HangoutLocation { get; set; }

       
    }
}
